#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/11/26 17:11
@desc
"""

import re
from ast import literal_eval

ana = {}
ana_basedata5 = {}

basedata_rr = re.compile(r'basedata5', re.I)
basedatatable_rr = re.compile(r'^tb_bass_\d{5}_(DAY|MON)$', re.I)
basedatatables = []
basedatatables_ct_phone = []
phone_no_list = ['MSISDN', 'PHONE_NO']

def csum(key, value):
    count, perm = ana.get(key, (0, 0))
    ana[key] = (count + 1, perm + value)


def sum_basedata5(key, value):
    count, perm = ana.get(key, (0, 0))
    ana[key] = (count + 1, perm + value)


def filterbasedata5(basedataname, tablename, columnnames):
    if basedata_rr.match(basedataname) and basedatatable_rr.match(tablename):
        basedatatables.append((tablename, columnnames))


def contain(columns, contain_str):
    res = 0
    for c in columns:
        for t in contain_str:
            if re.match('^%s$' % t, c, re.I):
                # print(c, t)
                res += 1
    return res


def contain_distinct(columns, contain_str):
    ret = contain(columns, contain_str)
    return True if ret else False


def print_dict(dic):
    for k in dic:
        print(k, ' -> ', dic.get(k))


def str_2_list(str):
    b = literal_eval(str)
    c = []
    if type(b).__name__ == 'str':
        c.append(b)
    else:
        c = b
    return list(c)


def priv_ana():
    with open(r'D:\ETL\priv_ana\priv_ana.txt', 'r') as f:
        num = 0
        for line in f:
            num += 1
            if num == 1:
                continue
            databasename, tablename, columnnames, scp = line.strip().split('\t')
            databasename = databasename.strip()
            tablename = tablename.strip()
            columnnames = columnnames.strip()
            scp = scp.strip()
            # print(databasename, tablename, columnnames, scp)
            csum(databasename, int(scp))
            filterbasedata5(databasename, tablename, columnnames)

    print_dict(ana)
    print('---------------------------')
    nums = (i[0] for i in ana.values())
    perms = (i[1] for i in ana.values())
    print(sum(nums))
    total_perms = sum(perms)
    print(total_perms, total_perms/1024.00/1024.00/1024.00/1024.00)

    # total_perm = sum(ana.values())
    # print(total)
    print('---------------------------')

    print(basedatatables)
    print(len(basedatatables))

    for bdt in basedatatables:
        print(bdt)
        columns_list = str_2_list((bdt[1]))
        contain_phone = contain_distinct(columns_list, phone_no_list)
        if contain_phone:
            basedatatables_ct_phone.append((bdt[0], columns_list))

    print(basedatatables_ct_phone)
    print(len(basedatatables_ct_phone))


if __name__ == '__main__':
    priv_ana()

    # cols = ('MSISDN', 'phone_no')
    # tar_cols = ('MSISDN', 'PHONE_NO')
    # res = contain(cols, tar_cols)
    # print(res)

